package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
)

type pInfo struct {
	Id   int    `json:"id"`
	Name string `form:"name" json:"name"`
	Age  int    `form:"age" json:"age"`
	Sid  string `json:"sid"`
	Old  bool   `json:"old"`
}

type PersonController struct {
	mainController
}

func (c *PersonController) Post() {

	res := map[string]interface{}{
		"ret":  0,
		"msg":  "succ",
		"data": nil,
	}

	defer func() {
		if err := recover(); err != nil {
			res["msg"] = err
		}
		c.Data["json"] = res
		c.ServeJSON()
	}()

	//var ps []models.Person
	//for i := 0; i < 1000; i++ {
	//	_i := strconv.Itoa(i)
	//	p := models.Person{
	//		Name: "jay-" + _i,
	//		Age:  rand.Intn(100),
	//		Sid:  "id-" + _i,
	//	}
	//	ps = append(ps, p)
	//}
	//
	//id, err := c.o.InsertMulti(len(ps), ps)
	//
	//if err == nil {
	//	beego.Info(id)
	//}

}

func (c *PersonController) Put() {

	res := map[string]interface{}{
		"ret":  0,
		"msg":  "put",
		"data": nil,
	}

	defer func() {
		if err := recover(); err != nil {
			res["msg"] = err
		}
		c.Data["json"] = res
		c.ServeJSON()
	}()

	p := orm.Params{
		"name": "7266111",
	}

	beego.Info(p)

	id, err := c.o.QueryTable("person").Filter("id", 7266).Update(p)

	if err == nil {
		beego.Info(id)
	} else {
		beego.Error(err)
	}

}

func (c *PersonController) Get() {
	res := map[string]interface{}{
		"ret":  0,
		"msg":  "succ",
		"data": nil,
	}

	defer func() {
		if err := recover(); err != nil {
			logs.Error(err)
			res["msg"] = err
		}
		c.Data["json"] = res
		c.ServeJSON()
	}()

	//querybuild

	//var params []interface{}
	//

	qb, _ := orm.NewQueryBuilder("mysql")

	qb.Select("*").From("person").Where("1=1")

	if _, ok := c.Input()["ids"]; ok {
		ids := c.GetString("ids")
		cond := fmt.Sprintf("find_in_set(%s,ids)", ids)
		qb.And(cond)

	}

	//qb.And("age>30")
	//qb.Limit(1)
	//var data []models.Person

	var data []orm.Params


	sql := qb.String()

	beego.Info(sql)

	n,err :=c.o.Raw(sql).Values(&data)
	//n,err :=o.Raw(sql).RowsToMap(&data,"name","sid")


	if err == nil{
		beego.Info(n)
		res["data"] = data
	}else{
		beego.Error(err)
	}

	c.Ctx.Request.Context()


	//ORM


	/*var data []*models.Person

	qs := c.o.QueryTable("person")

	if id, err := c.GetInt("id"); err == nil {
		qs = qs.Filter("id", id)
	}

	if _, ok := c.Input()["name"]; ok {
		qs = qs.Filter("name", c.GetString("name"))
	}

	qs.All(&data)

	if _, ok := c.Input()["id"]; ok {

		mapResult, err := utils.Struct2Maps(data)

		if err != nil {
			beego.Error(err)
			res["msg"] = err
			return
		}

		for i, d := range mapResult {
			if int(d["age"].(float64)) > 30 {
				mapResult[i]["old"] = true
			} else {
				mapResult[i]["old"] = false
			}

		}
		res["data"] = mapResult
	} else {
		res["data"] = data
	}*/

	///*var data []orm.Params
	//qs := c.o.QueryTable("person")
	//qs = qs.Limit(10000000000)
	//qs.Values(&data)*/
	//
	///*var data []models.Person
	//qs := c.o.QueryTable("person")
	//qs = qs.Limit(10000000000)
	//qs.All(&data)
	//mapResult, err := utils.Struct2Maps(data)
	//if err != nil{
	//	beego.Error(err)
	//	res["msg"] = err
	//	return
	//}*/
	//res["data"] = data
}
